﻿namespace WebMarkupMin.Example.Mvc.ViewModels.Settings
{
	using System.Web.Mvc;
	using System.Collections.Generic;
	using System.ComponentModel.DataAnnotations;
	using System.Linq;

	using Core;
	using Core.Constants;
	using Resources;

	/// <summary>
	/// Common HTML minification settings view model
	/// </summary>
	public abstract class CommonHtmlMinificationSettingsViewModel
	{
		private static readonly IEnumerable<SelectListItem> _whitespaceMinificationModes;
		private static readonly IEnumerable<SelectListItem> _availableCssMinifierList;
		private static readonly IEnumerable<SelectListItem> _availableJsMinifierList;


		[Display(Name = "DisplayName_WhitespaceMinificationMode", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public WhitespaceMinificationMode WhitespaceMinificationMode
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveHtmlComments", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveHtmlComments
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveHtmlCommentsFromScriptsAndStyles", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveHtmlCommentsFromScriptsAndStyles
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_UseShortDoctype", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool UseShortDoctype
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_UseMetaCharsetTag", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool UseMetaCharsetTag
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveTagsWithoutContent", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveTagsWithoutContent
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveEmptyAttributes", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveEmptyAttributes
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveRedundantAttributes", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveRedundantAttributes
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveHttpProtocolFromAttributes", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveHttpProtocolFromAttributes
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveHttpsProtocolFromAttributes", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveHttpsProtocolFromAttributes
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_RemoveJsProtocolFromAttributes", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool RemoveJsProtocolFromAttributes
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_MinifyEmbeddedCssCode", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool MinifyEmbeddedCssCode
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_MinifyInlineCssCode", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool MinifyInlineCssCode
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_CssMinifierName", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public string CssMinifierName
		{
			get;
			set;
		}

		public IEnumerable<SelectListItem> AvailableCssMinifierList
		{
			get { return _availableCssMinifierList; }
		}

		[Display(Name = "DisplayName_MinifyEmbeddedJsCode", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool MinifyEmbeddedJsCode
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_MinifyInlineJsCode", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool MinifyInlineJsCode
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_JsMinifierName", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public string JsMinifierName
		{
			get;
			set;
		}

		public IEnumerable<SelectListItem> AvailableJsMinifierList
		{
			get { return _availableJsMinifierList; }
		}

		[Display(Name = "DisplayName_ProcessableScriptTypeList", ResourceType = typeof(CommonHtmlMinificationStrings))]
		[StringLength(256)]
		public string ProcessableScriptTypeList
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_MinifyKnockoutBindingExpressions", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool MinifyKnockoutBindingExpressions
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_MinifyAngularBindingExpressions", ResourceType = typeof(CommonHtmlMinificationStrings))]
		public bool MinifyAngularBindingExpressions
		{
			get;
			set;
		}

		[Display(Name = "DisplayName_CustomAngularDirectiveList", ResourceType = typeof(CommonHtmlMinificationStrings))]
		[StringLength(256)]
		public string CustomAngularDirectiveList
		{
			get;
			set;
		}


		static CommonHtmlMinificationSettingsViewModel()
		{
			_whitespaceMinificationModes = new List<SelectListItem>
			{
				new SelectListItem
				{
					Value = WhitespaceMinificationMode.None.ToString(),
					Text = CommonHtmlMinificationStrings.ListItem_WhitespaceMinificationMode_None
				},
				new SelectListItem
				{
					Value = WhitespaceMinificationMode.Safe.ToString(),
					Text = CommonHtmlMinificationStrings.ListItem_WhitespaceMinificationMode_Safe
				},
				new SelectListItem
				{
					Value = WhitespaceMinificationMode.Medium.ToString(),
					Text = CommonHtmlMinificationStrings.ListItem_WhitespaceMinificationMode_Medium
				},
				new SelectListItem
				{
					Value = WhitespaceMinificationMode.Aggressive.ToString(),
					Text = CommonHtmlMinificationStrings.ListItem_WhitespaceMinificationMode_Aggressive
				}
			};

			_availableCssMinifierList = WebMarkupMinContext.Current.Code.GetCssMinifierRegistry()
				.Where(m => m.Value.Name != MinifierName.NullCssMinifier)
				.Select(m => new SelectListItem
					{
						Value = m.Value.Name,
						Text = m.Value.DisplayName
					}
				)
				;

			_availableJsMinifierList = WebMarkupMinContext.Current.Code.GetJsMinifierRegistry()
				.Where(m => m.Value.Name != MinifierName.NullJsMinifier)
				.Select(m => new SelectListItem
					{
						Value = m.Value.Name,
						Text = m.Value.DisplayName
					}
				)
				;
		}

		public IEnumerable<SelectListItem> WhitespaceMinificationModeList
		{
			get { return _whitespaceMinificationModes; }
		}


		protected CommonHtmlMinificationSettingsViewModel()
		{
			CssMinifierName = MinifierName.KristensenCssMinifier;
			JsMinifierName = MinifierName.CrockfordJsMinifier;
		}
	}
}